<?php
/**
 * Created by PhpStorm.
 * User: wuxiaofei
 * Date: 2018/5/25
 * Time: 9:42
 */
namespace V1;

class PlayPut extends \ResourceClass
{
    public $isActive = true;
    public $remark = '参与竞猜';

    public function post(array $req, array $post, &$result){
        if (!isset($req["uid"]) || !$req['uid']) {
            return "uid is not null";
        }
        $checkRes = $this->autoCheck($req['uid']);
        if($checkRes['code'] !== 200){
            return $checkRes['message'];
        }
        if (!array_key_exists('payload', $post)) {
            return 'require payload';
        }
        if(!$post['payload']){
            return 'data error';
        }
        $data = base64_decode($post['payload']);
        $data = json_decode($data,true);

        $uid = $req['uid'];
        $showing_id = $data['showing_id'];
        $target_id = $data['target_id'];
        if(!$showing_id){
            return 'payload is error';
        }
        if(!$target_id){
            return 'payload is error';
        }
        $targetArr = json_decode($target_id,true);
        if(!is_array($targetArr) || count($targetArr) < 1){
            return 'payload is error';
        }

        //Redis
        $redis_instance = \Util\Redis::getInstance();
        //赛程信息
        $showing_list = $redis_instance->get('ziyan_showing_list');
        $showing_list = json_decode($showing_list,true);

        //竞猜选项
        $setting_list = $redis_instance->get('ziyan_setting_list');
        $setting_list = json_decode($setting_list,true);

        $showingInfo = $showing_list[$showing_id];
        $thisTime = date('Y-m-d H:i:s');
        //$thisTime = '2018-06-15 10:00:00';

        if($showingInfo['open_time'] > $thisTime){
            $result = 5;
            return '该场比赛竞猜尚未开放！';
        }
        if($showingInfo['stop_time'] < $thisTime){
            $result = 5;
            return '该场比赛竞猜已截止';
        }
        if((int)$showingInfo['status'] !== \Biz\Play\Showings::STATUS_WAIT){
            $result = 5;
            return '比赛已结束，无法竞猜';
        }

        //用户竞猜
        $userActive = \Biz\Play\Active::getList('uid='.$uid.' and play_show_id='.$showing_id,1,0);
        if($userActive['lists']){
            $result = 5;
            return '你已参与过本场竞猜';
        }

        //是否有竞猜机会
        //当天已参与的次数 分享记录数
        $thisDate = date('Y-m-d');
        $activeNum = \Biz\Play\Active::getTotal('uid='.$uid.' and put_date="'.$thisDate.'"');
        $shareNum = \Biz\SharingLog::getShareByUidAndTime($uid,$thisDate);
        $shareTime = 0;
        if(isset($shareNum[0]['num'])) {
            $shareTime = ((int)$shareNum[0]['num'] >= (\Util\Config::get('max_chance_limit') - 1)) ? \Util\Config::get('max_chance_limit') : ((int)$shareNum[0]['num']+1);
        }
        if ((int)$activeNum === \Util\Config::get('max_chance_limit')) {
            $result = 2;
            return '今天竞猜机会已使用完';
        }

        //竞猜失败，没有机会竞猜，需要分享
        if ($activeNum >= $shareTime) {
            $result = 1;
            return '嘿~传球时刻到了';
        }

        if((int)$activeNum < (\Util\Config::get('max_chance_limit') - 1)){
            $result = 3;
            if($shareTime >= \Util\Config::get('max_chance_limit')){
                $result = 5;
            }
        }
        if((int)$activeNum === (\Util\Config::get('max_chance_limit') - 1)){
            $result = 4;
        }

        $baseModel = new \Base\BaseOrmModel();
        $baseModel->beginTransaction();
        try{
            foreach ($targetArr as $value){
                $insertData = array(
                    'uid' => $uid,
                    'play_show_id' => $showing_id,
                    'play_type' => $setting_list[$value]['type'],
                    'play_target_id' => $value,
                    'multiple' => 1,
                    'put_date' => date('Y-m-d'),
                    'status' => \Biz\Play\Active::STATUS,
                );
                $is_insert = \Biz\Play\Active::insert($insertData);
                if(!$is_insert['isok']){
                    throw new \Exception($is_insert['msg']);
                }
            }
            $baseModel->commit();
        }catch (\Exception $e){
            $baseModel->rollBack();
            return $e->getMessage();
        }
    }
}